using System.Collections;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace DefaultNamespace.Skill
{
    public class SkillSlot : MonoBehaviour, IPointerClickHandler, IPointerEnterHandler, IPointerExitHandler
    {
        [Header("UI References")]
        [SerializeField]
        private Image skillIcon;
        [SerializeField]
        private Image cooldownOverlay;
        [SerializeField]
        private Text keybindText;
        [SerializeField]
        private Text cooldownText;
        [Header("Settings")]
        [SerializeField]
        private KeyCode hotkey;
        [SerializeField]
        private Color readyColor = Color.white;
        [SerializeField]
        private Color cooldownColor = new Color(0.5f, 0.5f, 0.5f, 0.7f);
        private Skill assignedSkill;
        private bool isOnCooldown = false;
        public void Initialize(Skill skill, KeyCode hotkey = KeyCode.None)
        {
            assignedSkill = skill;
            this.hotkey = hotkey;
            
            // Setup UI
            if (skillIcon != null) skillIcon.sprite = skill.icon;
            if (keybindText != null && hotkey != KeyCode.None)
            {
                keybindText.text = hotkey.ToString().Replace("Alpha", "").Replace("KeyCode.", "");
            }
            UpdateVisuals();
        }
        private void Update()
        {
            if (assignedSkill == null) return;
            
            // Handle hotkey press
            if (hotkey != KeyCode.None && Input.GetKeyDown(hotkey))
            {
                TryCastSkill();
            }
            
            // Update cooldown visuals
            if (isOnCooldown)
            {
                UpdateCooldownVisual(assignedSkill.currentCooldown / assignedSkill.cooldown);
                if (assignedSkill.IsReady())
                {
                    isOnCooldown = false;
                    UpdateVisuals();
                }
            }
        }
        public void UpdateCooldown(float cooldownPercentage)
        {
            UpdateCooldownVisual(cooldownPercentage);
        }
        private void UpdateCooldownVisual(float cooldownPercentage)
        {
            if (cooldownOverlay != null)
            {
                cooldownOverlay.fillAmount = cooldownPercentage;
            }
            if (cooldownText != null)
            {
                cooldownText.text = assignedSkill.currentCooldown > 0
                    ? Mathf.Ceil(assignedSkill.currentCooldown).ToString()
                    : "";
            }
        }
        private void UpdateVisuals()
        {
            if (assignedSkill == null) return;
            
            // Set color based on cooldown state
            skillIcon.color = assignedSkill.IsReady() ? readyColor : cooldownColor;
            
            // Initial cooldown setup
            isOnCooldown = !assignedSkill.IsReady();
            UpdateCooldownVisual(assignedSkill.currentCooldown / assignedSkill.cooldown);
        }
        public void TryCastSkill()
        {
            if (assignedSkill != null && assignedSkill.IsReady())
            {
                // Get the player or caster reference (adjust based on your architecture)
                GameObject caster = GameObject.FindGameObjectWithTag("Player");
                if (caster != null)
                {
                    assignedSkill.Activate(caster);
                    isOnCooldown = true;
                    UpdateVisuals();
                    
                    // Play UI feedback
                    StartCoroutine(PlayCastFeedback());
                }
            } else
            {
                // Play error feedback
                StartCoroutine(PlayErrorFeedback());
            }
        }
        private IEnumerator PlayCastFeedback()
        {
            // Example: Small scale punch effect
            Vector3 originalScale = transform.localScale;
            transform.localScale = originalScale * 0.9f;
            yield return new WaitForSeconds(0.1f);
            transform.localScale = originalScale;
        }
        private IEnumerator PlayErrorFeedback()
        {
            // Example: Shake effect when skill can't be cast
            float shakeDuration = 0.3f;
            float shakeMagnitude = 5f;
            Vector3 originalPos = transform.localPosition;
            float elapsed = 0f;
            while (elapsed < shakeDuration)
            {
                float x = Random.Range(-1f, 1f) * shakeMagnitude;
                float y = Random.Range(-1f, 1f) * shakeMagnitude;
                transform.localPosition = originalPos + new Vector3(x, y, 0);
                elapsed += Time.deltaTime;
                yield return null;
            }
            transform.localPosition = originalPos;
        }
        
        // UI Interaction Handlers
        public void OnPointerClick(PointerEventData eventData)
        {
            if (eventData.button == PointerEventData.InputButton.Left)
            {
                TryCastSkill();
            }
        }
        public void OnPointerEnter(PointerEventData eventData)
        {
            // Show tooltip
            //SkillTooltip.Instance.ShowTooltip(assignedSkill);
        }
        public void OnPointerExit(PointerEventData eventData)
        {
            // Hide tooltip
           // SkillTooltip.Instance.HideTooltip();
        }
    }
}